//给easyui创建扩展属性
var sysbase = sysbase || {};
/**
 * 封装alert对话框
 * @param Msg
 * @param CallBack
 * @param success
 * @param message
 */
sysbase.alert = function(Message, CallBack, Success){
    parent.$.messager.alert('系统提示', Message, 'info', function(){
        if(typeof CallBack === 'function'){
            CallBack.call(this, Success, Message);
        }
    });
}

/**
 * 封装confirm对话框
 * @param msg
 * @param fn
 */
sysbase.confirm = function(msg, fn){
    parent.$.messager.confirm('系统信息', msg, fn);
}
sysbase.progress = function(type){parent.$.messager.progress(type);}

/**
 * 封装dialog弹出框，默认以iframe方式加载
 * @param options
 * @returns {*|$}
 */
sysbase.dialog = function(options){
    var _iframe = false,
        _iframeWindow = false,
        _iframeBody = false,
        _dg = parent.$('<div/>');

    var opts = $.extend({
        title : '&nbsp;',
        width : 640,
        height : 480,
        modal : true,
        handler: false,
        maximizable: false,
        onClose : function() {
            if(_iframe)
                _dg.find('[name="' + _iframe + '"]').remove();
            _dg.dialog('destroy');
        },
        buttons: [{
            text: '确定',
            iconCls: 'icon-ok',
            handler : function() {
                if(_iframe){
                    intWindow();
                    var _form = _iframeBody.find('#dialog-form');
                    if(_form.length > 0){
                        var _url = _form.attr('action');
                        _iframeWindow.sysbase.submitForm(_url, 'dialog-form', function(success){
                            if(success < 1) return;
                            _dg.dialog('close');
                            try{
                                opts.handler.call(this, success);
                            }catch (e){}
                        });
                    }else{
                        try{
                            var data = _iframeWindow.onConfirm();
                            try{
                                if(!data) return;
                                try{
                                    opts.handler.call(this, data);
                                }catch(e){}
                            }catch(e){}
                            _dg.dialog('close');
                        }catch(e){}
                    }
                    return;
                }
            }
        },{
            text: '取消',
            handler: function(){
                if(_iframe){
                    intWindow();
                    try{
                        _iframeWindow.onCancel();
                    }catch(e){}
                }
                _dg.dialog('close');
            }
        }],
        toolbar: false
    }, options);
    opts.cache = false; //强制不缓存数据
    if (opts.href) {
        _iframe = 'iframe-' + Math.random();
        opts.content = '<iframe name="' + _iframe + '" src="' + opts.href + '" allowTransparency="true" scrolling="auto" width="100%" height="100%" frameBorder="0"></iframe>';
        delete opts.href; //强制变更网址
    }
    _dg.dialog(opts);

    var intWindow = function(){
        _iframeWindow = _iframeWindow || _dg.find('[name="' + _iframe + '"]').get(0).contentWindow;
        _iframeBody = _iframeBody || $(_iframeWindow.document.body);
    };
    return _dg;
}

/**
 * 提交表单
 * @param Url
 * @param ItemObj
 * @constructor
 */
sysbase.submitForm = function(Url, ItemObj, CallBack){
    ItemObj = sysbase.makeObject(ItemObj);
    sysbase.progress();	// display the progress bar
    ItemObj.form('submit', {
        url: Url,
        onSubmit: function(){
            var isValid = $(this).form('validate');
            if (!isValid){
                sysbase.progress('close');	// hide progress bar while the form is invalid
            }
            return isValid;	// return false will stop the form submission
        },
        success: function(result){
            sysbase.progress('close');	// hide progress bar while submit successfully
            result = $.parseJSON(result);
            if(typeof result == 'object' && result.message){
                if(result.message.length > 0)
                    sysbase.alert(result.message, CallBack, parseInt(result.success));
                else
                    CallBack.call(this, parseInt(result.success));
            }
        }
    });
}

/**
 * 创建对象
 * @param itemObj
 * @returns {*|jQuery|HTMLElement}
 */
sysbase.makeObject = function(itemObj){
    if(typeof(itemObj) == 'string' && itemObj.indexOf('#') < 0 && itemObj.indexOf('.') < 0){
        itemObj = '#' + itemObj;
    }
    return $(itemObj);
}

/**
 * 全选树
 * @param _e
 */
sysbase.treeCheckAll = function(_e){
    var _e = sysbase.makeObject(_e);
    var roots = _e.tree('getRoots');
    for(var i = 0; i < roots.length; i++){
        _e.tree('check', roots[i].target);
    }
}

/**
 * 反选树
 * @param _e
 */
sysbase.treeUnCheckAll = function(_e){
    var _e = sysbase.makeObject(_e);
    var roots = _e.tree('getRoots');
    for(var i = 0; i < roots.length; i++){
        _e.tree('uncheck', roots[i].target);
    }
}

/**
 * 发起ajax请求，并返回数据
 * @param url
 * @param data
 * @param callback
 * @param type
 * @returns {*}
 */
sysbase.ajaxCall= function(url, data, callback, calltype, returntype) {
    if(typeof calltype == 'undefined') calltype = 'post';
    calltype = calltype.toLowerCase();
    if(typeof returntype == 'undefined') returntype = 'json';

    var result = null;
    switch(calltype){
        case 'get':
            $.get(url, data, function(res){
                result = res;
            }, returntype);
            break;
        default :
            $.post(url, data, function(res){
                result = res;
            }, returntype);
            break;
    }

    try{
        callback.call(this, result);
    }finally{
        return result;
    }
}